/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	  深圳市国省检日常化监管系统       </p>
 * <p>创建者   :	heyong 
 * 
 * <p>描   述  :   DovelopIndicatorsServiceIpml.java for com.pengtu.gsj.service.impl.check.jhgl    </p>
 * 
 * <p>最后修改 : $: 2017年7月20日-下午2:35:52 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jhgl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jhgl.DovelopIndicatorsDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jhgl.DevelopTarget;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jhgl.DovelopIndicatorsService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * DovelopIndicatorsServiceIpml
 * 
 * 2017年7月20日 下午2:35:52
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class DovelopIndicatorsServiceIpml implements DovelopIndicatorsService {
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	public DovelopIndicatorsDao dovelopIndicatorsDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	public List<DevelopTarget> getDevelopTargetList(QueryFilter queryFilter){
		return dovelopIndicatorsDao.find(queryFilter, "year", true);
	}
	
	@Override
	public Map<String, Object> getDevelopTarget(String stratTime, String endTime, CheckTable checkTable, String tag, String type) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<DevelopTarget> developList = new ArrayList<DevelopTarget>();
		List<Attachment> list = new ArrayList<Attachment>();
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull =  Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart =  Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		//查询数据
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("year", stratTime, MatchType.GE);
		queryFilter.addFilter("year", endTime, MatchType.LE);
		developList = this.getDevelopTargetList(queryFilter);
		//查询得分情况
		double total = 0.0;				//年度累计得分
		double percentage = 0.0;		//完善度
		double temp = 0.0;				//总分
		//返回时间
		int years[] = new int[Integer.parseInt(endTime) - Integer.parseInt(stratTime)+1];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(stratTime)+i;
		}
		//1.表示为计划管理模块下的发展指标 2表示为综合管理模块下发展指标下的poi占比
		int i = 0;
		if (type.equals("2")) {
			if (developList.size() > 0) {
				i = developList.size() - 1;
			}
		} else if (type.equals("1")) {
			i = 0;
		}
		
		for (;i < developList.size(); i++) {
			if (developList.get(i).getPqiAverage() >= 80.0 && developList.get(i).getPqiPate() < 12.0) {
				total = poitMark;
				temp = total;
				developList.get(i).setAverageMark(0.00);
				developList.get(i).setPateMark(0.00);
			} else {
				double averageMark = MathsUtils.subDouble(80.0, developList.get(i).getPqiAverage());
				if (averageMark > 0.0) {
					averageMark = MathsUtils.mulDouble(averageMark*10, yearFull);
					averageMark = MathsUtils.formatDouble(averageMark,2);
					if(averageMark > poitMark){
						averageMark = poitMark;
					} 
				} else {
					averageMark = 0.00;
				}
				developList.get(i).setAverageMark(averageMark);
				
				double pateMark = MathsUtils.subDouble(developList.get(i).getPqiPate(),12.00);
				pateMark = MathsUtils.formatDouble(pateMark,6);
				if	(pateMark > 0) {
					pateMark =MathsUtils.mulDouble(pateMark*1000,yearPart);
					if (pateMark > poitMark) {
						pateMark = poitMark;
					}
				} else {
					pateMark = 0.00;
				}
				developList.get(i).setPateMark(pateMark);
				
				//年度得分
				if (MathsUtils.formatDouble(MathsUtils.sumDouble(averageMark,pateMark),2) >= poitMark) {
					total = 0.00;
				} else {
					total = MathsUtils.sumDouble(averageMark,pateMark);
					total = MathsUtils.subDouble(poitMark, total);
					total = MathsUtils.formatDouble(total, 2);
				}
				temp = MathsUtils.sumDouble(temp,total);
			}
			developList.get(i).setYearMark(total);
		}
		
		//判断完善度
		double sum = 0;
		if (type.equals("1")) {
			percentage = MathsUtils.mulDouble(poitMark, years.length);
		} else if (type.equals("2")){
			percentage = poitMark;
		}
		sum = percentage;
		percentage = MathsUtils.divDouble(temp, percentage, 4);
		percentage  = MathsUtils.formatDouble(percentage*100, 2);
		map.put("percent", percentage);
						
		//得到所有文件
		list = systemManageService.getAllCheckAttachments(stratTime, endTime, tag);
		checkTableService.updateCheckTable(checkTable, percentage, temp, sum, true);
		map.put("att", list);
		map.put("checkTable", checkTable);
		map.put("years", years);
		map.put("sum", temp);
		map.put("develop", developList);
		map.put("score", checkTable.getTestFull());
		return map;
	}
}
